<template>
  <div>
    <el-dialog
    v-model="dialogVisible"
    title="新增"
    width="60%"
    :before-close="handleClose"
  >
    <div class="contents">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
		  <el-form-item label="关联销售单号" class="inputs">
		     <el-input v-model="formInline.salesOrderNo" placeholder="请输入供应商名称" clearable />
		  </el-form-item>
		  <el-form-item label="版本" class="inputs">
		     <el-input v-model="formInline.version" placeholder="请输入供应商名称" clearable />
		  </el-form-item>
      <el-form-item label="负责人" prop="ownerUserId">
        <el-select style="width: 180px;"
          v-model="formInline.responsiblePerson">
          <el-option
            v-for="item in userOptions"
            :key="item.id"
            :label="item.nickname"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
  <el-form-item label="工单状态" class="inputs">
   <el-radio-group v-model="formInline.workOrderStatus">
   <el-radio value="1">使用</el-radio>
   <el-radio value="2">禁用</el-radio>
   </el-radio-group>
  </el-form-item>
      <el-form-item label="产品名称" class="inputs">
         <el-input v-model="formInline.productName" placeholder="请输入供应商名称" clearable />
      </el-form-item>
      <el-form-item label="客户编码" class="inputs">
         <el-input v-model="formInline.customerCode" placeholder="请输入供应商名称" clearable />
      </el-form-item>
	  <el-form-item label="客户名称" class="inputs">
	     <el-input v-model="formInline.customerName" placeholder="请输入供应商名称" clearable />
	  </el-form-item>
      <el-form-item label="领料状态" class="inputs">
       <el-radio-group v-model="formInline.materialPickingStatus">
       <el-radio value="1">未领料</el-radio>
	   <el-radio value="2">已领料</el-radio>
       </el-radio-group>
      </el-form-item>
      <el-form-item label="产品规格" class="inputs">
         <el-input v-model="formInline.productSpecification" placeholder="请输入供应商名称" clearable />
      </el-form-item>
	  <el-form-item label="入库状态" class="inputs">
	   <el-radio-group v-model="formInline.warehousingStatus">
	   <el-radio value="1">未入库</el-radio>
	   <el-radio value="2">已入库</el-radio>
	   </el-radio-group>
	  </el-form-item>
      <el-form-item label="计划数量" class="inputs">
         <el-input v-model="formInline.plannedQuantity" placeholder="请输入供应商名称" clearable />
      </el-form-item>
      <el-form-item label="单位" class="inputs">
         <el-input v-model="formInline.unit" placeholder="请输入供应商名称" clearable />
      </el-form-item>
	  <el-form-item label="机台状态" class="inputs">
	   <el-radio-group v-model="formInline.machineStatus">
	   <el-radio value="1">机台状态1</el-radio>
	   <el-radio value="2">机台状态2</el-radio>
	   </el-radio-group>
	  </el-form-item>
	  <el-form-item label="已入库数量" class="inputs">
	     <el-input v-model="formInline.warehousedQuantity" placeholder="请输入供应商名称" clearable />
	  </el-form-item>
	  <el-form-item label="良品数量" class="inputs">
	     <el-input v-model="formInline.goodQuantity" placeholder="请输入供应商名称" clearable />
	  </el-form-item>
	  <el-form-item label="生产工序数" class="inputs">
	     <el-input v-model="formInline.productionProcessCount" placeholder="请输入供应商名称" clearable />
	  </el-form-item>
	  <el-form-item label="外协工序数" class="inputs">
	     <el-input v-model="formInline.outsourcingProcessCount" placeholder="请输入供应商名称" clearable />
	  </el-form-item>
	  <el-form-item label="外协状态" class="inputs">
	   <el-radio-group v-model="formInline.outsourcingStatus">
	   <el-radio value="1">外协状态1</el-radio>
	   <el-radio value="2">外协状态2</el-radio>
	   </el-radio-group>
	  </el-form-item>
	  <el-form-item label="客户交期" class="inputs">
	  <el-time-picker
	    v-model="formInline.customerDeliveryDate"
	    value-format="HH:mm:ss"
	    placeholder="选择客户交期"
	  />
	  </el-form-item>
      <el-form-item label="产品备注" class="inputs">
         <el-input v-model="formInline.remark" placeholder="请输入供应商名称" clearable />
      </el-form-item>
      
      </el-form>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="btnAdd">
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>
  </div>
</template>
<script lang="ts" setup>
	import * as UserApi from '@/api/system/user'
import {productApi} from '@/api/erp/foundation/product'
import { ref, watch, reactive,nextTick, onMounted ,onBeforeUnmount, computed} from "vue";
const dialogVisible = ref(false);
const emit = defineEmits(['update:modelValue', 'confirm'])
const initialState = () => ({
  id: "",
  name: "",
  barCode:"",
  categoryId:"",
  unitId:"",
  status:"",
  standard:"",
  remark:"",
  expiryDay:"",
  weight:"",
  purchasePrice:"",
  minPrice:"",
  contact:"",
  warehouseLocationId:"",
  warehouseAreaId:"",
  


})
const userOptions = ref<UserApi.UserVO[]>([]) // 用户列表
const formInline = reactive(initialState());
//新增
function btnAdd() {
  productUnitApi.create(formInline).then((res) => {
   
    if (res) {
      dialogVisible.value = false
      ElMessage({
        message: '操作成功',
        type: 'success'
      })
      // 触发确认事件（可选，用于父组件刷新）
      emit('confirm')
      // 关闭弹窗
     
    }
  })
}
// 关闭前处理（如点击右上角X）
const handleClose = (done) => {
  done() // 调用done关闭对话框
}

// 接收父组件参数
const props = defineProps({
		modelValue: {
			type: Boolean,
			default: false,
		},


	

	});
  watch(() => props.modelValue, (newVal) => {
  dialogVisible.value = newVal;
  if (newVal) {
    resetForm();
	// 获得用户列表
  }
});
const open = async () => {


  // 加载部门、用户列表
	userOptions.value =await  UserApi.getSimpleUserList()
 console.log( userOptions.value)
}
//重置表单
const resetForm = () => {
open()
  Object.assign(formInline, initialState());
  // 额外处理: 确保状态默认选中"已开启"
  formInline.status = "0";
}
//  在关闭时也重置表单
const closeDialog = () => {
  dialogVisible.value = false;
  resetForm(); // 关闭时重置
  emit('update:modelValue', false);
}


  watch(dialogVisible, (val) => {
  emit('update:modelValue', val)
})


</script>
<style scoped>
.m-2{
  position: relative;
  top: -9px;
}

.inputs{
  width: 260px;
}

.contents{

}
</style>